package com.zbcz.crm.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.zbcz.crm.bean.AuditBean

import com.zbcz.crm.databinding.ItemAuditLayoutBinding
import com.zbcz.crm.databinding.ItemTendarListBinding
import com.zbcz.crm.util.onSingleClick
import com.zbcz.crm.util.setRoundedCorners

/**
 * 审计
 */
class AuditBeanListAdapter(private val dataList: List<AuditBean>) :
    RecyclerView.Adapter<AuditBeanListAdapter.AuditAdapterHolder>() {

    private val expandedStates = mutableMapOf<Int, Boolean>()

    var onItemFileClickListener: ((Int, String) -> Unit)? = null

    var onItemMaterialClickListener: ((Int, String) -> Unit)? = null

    inner class AuditAdapterHolder(private val binding: ItemAuditLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {

        init {
            binding.tvFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }
            binding.ivFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }

            binding.tvAuditFileValue.onSingleClick {
                onItemFileClickListener?.invoke(
                    adapterPosition,
                    dataList[adapterPosition].auditReport
                )
            }
        }


        fun bind(data: AuditBean) {
            binding.run {
                tvAuditCustomerMoneyValue.text = data.auditMoney
                tvAuditRatioValue.text = data.auditReductionProportion
                tvAuditCustomerMoneyValue.text = data.auditReductionMoney
            }
            binding.contentParent.setRoundedCorners(45f)
        }

        fun setExpanded(expanded: Boolean) {
            val visible = if (expanded) View.VISIBLE else View.GONE
            binding.clAuditFile.visibility = visible
            binding.clAuditMoney.visibility = visible
        }
    }

    private fun toggleItemExpansion(position: Int) {
        expandedStates[position] = !(expandedStates[position] ?: true)
        notifyItemChanged(position)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): AuditAdapterHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = ItemAuditLayoutBinding.inflate(inflater, parent, false)
        return AuditAdapterHolder(binding)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: AuditAdapterHolder, position: Int) {
        holder.bind(dataList[position])
        // 设置展开或折叠的状态
        holder.setExpanded(expandedStates[position] ?: true)
    }
}